package tanke;

import java.util.Vector;

/**
 * Created by bao on 2017/8/26.
 */
public class Enemy extends Tank implements Runnable {

    //存放敌人坦克
    Vector<Shot> enemyShots = new Vector<Shot>();
    int time = 0;

    public Enemy(int x, int y, int direct, int type, int speed) {
        super(x, y, direct, type, speed);
    }

    /**
     * 敌人坦克运动的线程
     */
    @Override
    public void run() {

        while (true) {
            int direct = (int) ((Math.random()) * 4);
            this.setDirect(direct + 1);
            switch (this.getDirect()) {
                case 1:
                    for (int i = 0; i < 30; i++) {
                        if (this.getY() > 0) {
                            this.setY(this.getY() - this.enemySpeed);
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        if (this.getY() < 270) {
                            this.setY(this.getY() + this.enemySpeed);
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        if (this.getX() > 0) {
                            this.setX(this.getX() - this.enemySpeed);
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                case 4:
                    for (int i = 0; i < 30; i++) {
                        if (this.getX() < 370) {
                            this.setX(this.getX() + this.enemySpeed);
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            this.time++;
            if(time%2==0){
                if(this.isLive()){
                    if(enemyShots.size()<5){
                        Shot shot = null;
                        switch(this.getDirect()){
                            case 1:
                                shot = new Shot(this.getX()+10, this.getY(), 1);
                                this.enemyShots.add(shot);
                                break;
                            case 2:
                                shot = new Shot(this.getX()+10, this.getY()+30, 2);
                                this.enemyShots.add(shot);
                                break;
                            case 3:
                                shot = new Shot(this.getX(), this.getY()+10, 3);
                                this.enemyShots.add(shot);
                                break;
                            case 4:
                                shot = new Shot(this.getX()+30, this.getY()+10, 4);
                                this.enemyShots.add(shot);
                                break;

                        }
                        //启动子弹线程
                        Thread t = new Thread(shot);
                        t.start();
                    }
                }
            }

            if (this.isLive()== false) {
                break;
            }
        }

    }
}